package com.ddone.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ddone.common.exception.Assert;
import com.ddone.common.result.ResponseEnum;
import com.ddone.finance.mapper.IntegralGradeMapper;
import com.ddone.finance.mapper.UserInfoMapper;
import com.ddone.finance.pojo.entity.BorrowInfo;
import com.ddone.finance.mapper.BorrowInfoMapper;
import com.ddone.finance.pojo.entity.IntegralGrade;
import com.ddone.finance.pojo.entity.UserInfo;
import com.ddone.finance.pojo.enums.BorrowInfoStatusEnum;
import com.ddone.finance.pojo.enums.BorrowerStatusEnum;
import com.ddone.finance.pojo.enums.UserBindEnum;
import com.ddone.finance.service.BorrowInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Optional;

/**
 * <p>
 * 借款信息表 服务实现类
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@Service
public class BorrowInfoServiceImpl extends ServiceImpl<BorrowInfoMapper, BorrowInfo> implements BorrowInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private IntegralGradeMapper integralGradeMapper;

    @Override
    public BigDecimal getBorrowAmount(Long userId) {
        Assert.notNull(userId,ResponseEnum.LOGIN_AUTH_ERROR);
        //获取用户积分
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Assert.notNull(userInfo, ResponseEnum.LOGIN_MOBILE_ERROR);
        Integer integral = userInfo.getIntegral();

        LambdaQueryWrapper<IntegralGrade> wrapper = Wrappers.lambdaQuery(IntegralGrade.class);
        wrapper.le(IntegralGrade::getIntegralStart,integral)
                .ge(IntegralGrade::getIntegralEnd, integral)
                .last("limit 1");
        IntegralGrade integralGrade = integralGradeMapper.selectOne(wrapper);
        if(integralGrade == null){
            return new BigDecimal("0");
        }
        return integralGrade.getBorrowAmount();
    }

    @Override
    public void saveBorrowInfo(BorrowInfo borrowInfo, Long userId) {
        //获取userInfo的用户数据
        UserInfo userInfo = userInfoMapper.selectById(userId);
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        //判断用户绑定状态, 必须先绑定汇付宝
        Assert.isTrue(
                userInfo.getBindStatus().intValue() == UserBindEnum.BIND_OK.getStatus().intValue(),
                ResponseEnum.USER_NO_BIND_ERROR);
        //判断用户信息是否审批通过
        Assert.isTrue(userInfo.getBorrowAuthStatus().intValue() == BorrowerStatusEnum.AUTH_OK.getStatus().intValue(), ResponseEnum.USER_NO_AMOUNT_ERROR);

        //判断借款额度是否足够
        BigDecimal borrowAmount = this.getBorrowAmount(userId);
        Assert.isTrue(
                borrowInfo.getAmount().doubleValue() <= borrowAmount.doubleValue(),
                ResponseEnum.USER_AMOUNT_LESS_ERROR);
        //存储数据
        borrowInfo.setUserId(userId);
        //百分比转成小数
        borrowInfo.setBorrowYearRate( borrowInfo.getBorrowYearRate().divide(new BigDecimal(100)));
        borrowInfo.setStatus(BorrowInfoStatusEnum.CHECK_RUN.getStatus());
        baseMapper.insert(borrowInfo);
    }
}
